package com.treasure.mall.zbqmallmanager.controller;

import com.alibaba.fastjson.JSONArray;
import com.treasure.mall.biz.bo.BaseConfig;
import com.treasure.mall.biz.bo.config.BasicsConfig;
import com.treasure.mall.biz.bo.config.SignConfig;
import com.treasure.mall.biz.bo.config.SignRewardConfig;
import com.treasure.mall.biz.bo.config.ZbConfig;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.zbq.common.base.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author guomw
 * @date 2018/11/21
 */
@RequestMapping("/config")
@Controller
public class BaseConfigController {

    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private Environment environment;

    @RequestMapping("/getConfig")
    public String getBaseConfig(Model model) {
        BaseConfig baseConfig = baseConfigService.getConfig();
        model.addAttribute("config", baseConfig);
        return "config/baseConfig";
    }

    @RequestMapping("/ajax/save")
    @ResponseBody
    public ApiResult save(
            ZbConfig zbConfig,
            BasicsConfig basicsConfig
    ) {
        BaseConfig baseConfig = baseConfigService.getConfig();

        if (zbConfig.getAddFrequency() < 10) {
            zbConfig.setAddFrequency(10);
        }

        //甄宝设置
        baseConfig.getZbConfig().setAddFrequency(zbConfig.getAddFrequency());
        baseConfig.getZbConfig().setCurrentDjCount(zbConfig.getCurrentDjCount());
        baseConfig.getZbConfig().setToDayMaxAddCount(zbConfig.getToDayMaxAddCount());
        baseConfig.getZbConfig().setLiveNotice(zbConfig.getLiveNotice());
        //基本配置
        baseConfig.getBasicsConfig().setPayTimeoutCount(basicsConfig.getPayTimeoutCount());
        baseConfig.getBasicsConfig().setSecurityDepositFirst(basicsConfig.getSecurityDepositFirst());
        baseConfig.getBasicsConfig().setSecurityDepositTwo(basicsConfig.getSecurityDepositTwo());
        baseConfig.getBasicsConfig().setUpgradeAmount(basicsConfig.getUpgradeAmount());

        baseConfigService.save(baseConfig);
        return ApiResult.ok();
    }

    @RequestMapping("/ajax/save/sign")
    @ResponseBody
    public ApiResult saveSign(@RequestParam String rewardConfig, @RequestParam(defaultValue = "0") Long subscribeRewardIntegral) {
        SignConfig signConfig = new SignConfig();
        signConfig.setRewardConfig(JSONArray.parseArray(rewardConfig, SignRewardConfig.class));
        signConfig.setSubscribeRewardIntegral(subscribeRewardIntegral);
        baseConfigService.saveSignConfig(signConfig);
        return ApiResult.ok();
    }


}
